using System;
using System.Collections;

namespace runner
{
	/// <summary>
	/// Summary description for ChangeList.
	/// </summary>
	public class MigrationChangeList
	{
		private SortedList _list = new SortedList();

		public MigrationChangeList()
		{
		}

		public void Add(Migration migration) 
		{
			_list.Add(_list.Count, migration);
		}

		public void Remove(int key) 
		{
			_list.Remove(key);
		}

		public string[] Print(ChangeListType type, int version)
		{
			string[] string_array = new string[0];

			ArrayList list = CollectMigrations(type, version);
			if (list.Count > 0) 
			{
				string_array = new string[list.Count];
				int c = 0;
				foreach (string item in list) 
				{
					string_array[c++] = item;
				}
			}

			return string_array;
		}
		
		private ArrayList CollectMigrations(ChangeListType type, int version)
		{
			bool up = true;
			
			if (_list.Count > 1 && ((Migration)_list[0]).Version > version)
			{
				up = false;
			}
			
			ArrayList list = new ArrayList();
			foreach (Migration migration in _list.Values)
			{
				switch (type) {
					case ChangeListType.VERSIONS:
						list.Add("Version: " + migration.Version);
						break;
					case ChangeListType.NAMES:
						list.Add("Name: " + migration.Name);
						break;
					case ChangeListType.COMMENTS:
						list.Add(migration.Comment);
						break;
					case ChangeListType.ERRORS:
						list.Add(migration.Error);
						break;
					case ChangeListType.SCRIPTS:
						if (up)
							list.Add("Script: " + migration.Up);
						else
							list.Add("Script: " + migration.Down);
						break;
					case ChangeListType.ALL:
						list.Add("Version: " + migration.Version);
						list.Add("Name: " + migration.Name);
						list.Add("Comment: " + migration.Comment);
						list.Add("Error: " + migration.Error);
						
						if (up)
							list.Add("Script: " + migration.Up);
						else
							list.Add("Script: " + migration.Down);
						break;
				}
			}

			return list;
		}
	}
}
